package com.example.sport;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity2 extends AppCompatActivity {

    private static final String BASE_URL = "one_url";
    private EditText etPhone;
    private EditText etCode;
    private String strUsername;
    private String strPassword;
    private Button btnLogin;
    private Button btnGain;
    private spHelper sh;
    private Context mContext;
    private CheckBox cbRemember;
    private String isRemember;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mContext = getApplicationContext();   // 获取上下文
        sh = new spHelper(mContext);  // 实例化spHelper，传入上下文参数
        bindView();

        //注册
        btnGain=findViewById(R.id.user_gain);
        btnGain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phone = etPhone.getText().toString();
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                final HttpBin httpBin = retrofit.create(HttpBin.class);

                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("phone",phone);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = String.valueOf(jsonObject);
                MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
                RequestBody requestBody = RequestBody.create(mediaType,json);
                Call<ResponseBody> call = httpBin.gain(requestBody);
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            Log.i("TAG", "onResponse: " + response.body().string());
                            Toast.makeText(MainActivity2.this, "已发送 验证码三分钟内有效", Toast.LENGTH_SHORT).show();

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable throwable) {

                    }
                });

            }
        });
//        //登陆功能 不确定完成度以及功能设计不是很完美（后面临时加的）
//        btnLogin = findViewById(R.id.btn_login);
//        btnLogin.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String phone = etPhone.getText().toString();
//                Retrofit retrofit = new Retrofit.Builder()
//                        .baseUrl(BASE_URL)
//                        .addConverterFactory(GsonConverterFactory.create())
//                        .build();
//                final HttpBin httpBin = retrofit.create(HttpBin.class);
//
//                JSONObject jsonObject = new JSONObject();
//                try {
//                    jsonObject.put("phone",phone);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//                String json = String.valueOf(jsonObject);
//                MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
//                RequestBody requestBody = RequestBody.create(mediaType,json);
//                Call<ResponseBody> call = httpBin.login(requestBody);
//                call.enqueue(new Callback<ResponseBody>() {
//                    @Override
//                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//                        try {
//                            Log.i("TAG", "onResponse: " + response.body().string());
//                            Toast.makeText(MainActivity2.this, "登陆成功", Toast.LENGTH_SHORT).show();
//                            Intent intent= new Intent(MainActivity2.this,MainActivity3.class);
//                            startActivity(intent);
//
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                    @Override
//                    public void onFailure(Call<ResponseBody> call, Throwable throwable) {
//
//                    }
//                });
//            }
//        });
    }


    private void bindView() {
        etCode = findViewById(R.id.et_code);
        etPhone = findViewById(R.id.et_phone);
        btnLogin = findViewById(R.id.btn_login);
//        cbRemember = findViewById(R.id.cb_Remember);
        // 点击登录按钮事件
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(MainActivity2.this, "登陆成功", Toast.LENGTH_SHORT).show();
                Intent intent= new Intent(MainActivity2.this,MainActivity3.class);
                startActivity(intent);
            }
        });
    }



}